<?php
include_once '../model/user.php';

session_start();
// 登陆
function login()
{
    $username = $_POST['username'];
    $passwd = $_POST['passwd'];
    $result = dblogin($username, $passwd);

    if ($result == 1) {
        $_SESSION["username"] = $username;
        return 1;
    } else {
        return 0;
    }
}

// 检测用户登陆状态
function isValiUser()
{
    if (isset($_SESSION["username"])){
        $username = $_SESSION["username"];
        return $username;
    }else{
        return false;
    }
}

// 检测admin登陆状态
function isValiAdmin()
{
    if ($_SESSION["username"] == "admin"){
        $username = $_SESSION["username"];
        return $username;
    }else{
        return false;
    }
}

// 销毁Session，注销登陆状态
function logout()
{
    if (isset($_SESSION["username"])) {
        unset($_SESSION["username"]);
        setcookie("username", "", time() + 1, '/');
        return 1;
    } else {
        return 0;
    }
}
